/**
 * Copyright (C) 2011, mordredd666 [_at_] gmail.com
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package de.mordred.gui.einstellungen;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import de.mordred.Props;
import de.mordred.gui.FixedPanel;
import de.mordred.gui.TopLeiste;
import de.mordred.gui.VerticalFlowLayout;
import de.mordred.util.Version;

public class Einstellungen extends FixedPanel
{

	private static Einstellungen singleton;

	private static Object LOCK = new Object();

	private static final long serialVersionUID = -1436073372192767838L;

	private JCheckBox _random;

	private TopLeiste _topleiste;

	public static Einstellungen getInstance()
	{
		if (singleton == null)
		{
			synchronized (LOCK)
			{
				singleton = new Einstellungen();
			}
		}
		return singleton;
	}

	private Einstellungen()
	{

		this.setLayout(new BorderLayout());
		_topleiste = new TopLeiste("Einstellungen");
		this.add(_topleiste, BorderLayout.NORTH);

		JPanel center = new JPanel(new VerticalFlowLayout());
		this.add(center, BorderLayout.CENTER);

		_random = new JCheckBox("Fragen zufällig ordnen?");

		_random.setSelected(Props.getInstance().getBoolean(Props.RANDOM_FRAGEN));

		_random.addActionListener(getComboBoxListener());

		center.add(_random);

		String text = "<html><center><br><br><br>jPhysikum wurde <u><b>nicht</b></u> von Medi-Learn programmiert"
				+ "<br><br>Alle Fragen wurden von Medi-Learn erstellt</center></html>";

		center.add(new JLabel(text));

		JPanel south = new JPanel(new FlowLayout(FlowLayout.CENTER));
		south.add(new JLabel("Version " + Version.VERSION));
		add(south, BorderLayout.SOUTH);
		_topleiste.getPrevious().addActionListener(_action);
	}

	public boolean isRandom()
	{
		return _random.isSelected();
	}

	private ActionListener getComboBoxListener()
	{
		return new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				Props p = Props.getInstance();
				p.setProperty(Props.RANDOM_FRAGEN, isRandom());
			}
		};
	}

	private ActionListener _action = new ActionListener()
	{

		@Override
		public void actionPerformed(ActionEvent e)
		{
			Props p = Props.getInstance();
			p.setProperty(Props.RANDOM_FRAGEN, isRandom());
			_topleiste.getPrevious().removeActionListener(_action);
		}
	};

}
